package com.sphd.restaurantsystem.activitys.marking;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.info.TopUpInfo;
import com.sphd.restaurantsystem.presenter.CzPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.CzView;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 充值列表
 */
public class CzListActivity extends BaseActivity implements CzView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.cz_money_listview)
    ListView czMoneyListview;
    CzPresenterImpl czPresenter;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;

    List<String> strings;
    ArrayAdapter arrayAdapter;
    List<TopUpInfo> topUpInfos;
    @BindView(R.id.coupon_add)
    ImageView couponAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cz_list);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> finish());
        czMoneyListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (isCanDo()){
                    Toast.makeText(getApplicationContext(), "店长会去操作，您不用费心了", Toast.LENGTH_SHORT).show();
                }else {
                    delDialog(i);
                }
                return false;
            }
        });
    }

    private void delDialog(int i) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        // set dialog message
        alertDialogBuilder
                .setTitle("删除提示")
                .setMessage("你将删除本条充值信息")
                .setCancelable(false)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                czPresenter.delUrl(RestaurantUrl.Httptitle + RestaurantUrl.DelCzList
                                        + "id=" + topUpInfos.get(i).getId());
                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        czPresenter = new CzPresenterImpl(this);
        setTitleBar(toolbar,"充值服务");
        initAdapter();
        addData();
    }

    private void initAdapter() {
        topUpInfos=new ArrayList<>();
        strings = new ArrayList<>();
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
        czMoneyListview.setAdapter(arrayAdapter);
    }

    private void addData() {
        czPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.CzList + "cgid=" + restaurantInfo.getId());
    }

    @Override
    public void recList(List<TopUpInfo> topUpInfos) {
        this.topUpInfos.clear();
        this.topUpInfos=topUpInfos;
        strings.clear();
        for (TopUpInfo topUpInfo : topUpInfos) {
            strings.add("缴费" + topUpInfo.getReceipt_money() + "充值" +
                    topUpInfo.getAdd_money());
        }
        arrayAdapter.notifyDataSetChanged();
    }

    @OnClick(R.id.coupon_add)
    public void onClick() {
        if (isCanDo()){
            Toast.makeText(getApplicationContext(), "店长会去操作，您不用费心了", Toast.LENGTH_SHORT).show();
        }else {
            intent.setClass(context, AddCzInfoActivity.class);
            startActivityForResult(intent,1);
        }

    }

    private boolean isCanDo() {
        return (isCg_has_storemanager(restaurantInfo) && isSuperManager(loginInfo));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==0){
            addData();
        }
    }

    @Override
    public void recRes(int res) {
            if (res==1){
                toast("删除成功");
                addData();
            }else {
                toast("删除失败，请重试！");

            }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
